# Maintainer: Torsten Keßler <tpkessler at archlinux dot org>
# Contributor: Markus Näther <naetherm@informatik.uni-freiburg.de>
pkgname=rocalution
pkgver=5.7.1
pkgrel=1
pkgdesc='Next generation library for iterative sparse solvers for ROCm platform'
arch=('loong64' 'x86_64')
url='https://rocalution.readthedocs.io/en/master'
license=('MIT')
depends=('hip' 'rocsparse' 'rocblas' 'rocsolver' 'rocprim' 'rocrand')
makedepends=('rocm-cmake' 'openmp' 'openmpi')
optdepends=('openmp: Multithreaded CPU applications'
            'openmpi: Distributed CPU and multi GPU applications')
_git='https://github.com/ROCmSoftwarePlatform/rocALUTION'
source=("$pkgname-$pkgver.tar.gz::$_git/archive/rocm-$pkgver.tar.gz"
        "rocalution-disable-hip-property.patch")
sha256sums=('b95afa1285759843c5fea1ad6e1c1edf283922e0d448db03a3e1f42b6942bc24'
            '7335ce3fb8a8663cd2d393632fecab261239de3cbd00e7e7bbda2e1d217c7ec2')
options=(!lto)
_dirname="$(basename "$_git")-$(basename "${source[0]}" ".tar.gz")"

prepare() {
  cd "$_dirname"
  patch -Np1 -i "$srcdir/rocalution-disable-hip-property.patch"
}

build() {
  # Compile source code for supported GPU archs in parallel
  export HIPCC_COMPILE_FLAGS_APPEND="-parallel-jobs=$(nproc)"
  export HIPCC_LINK_FLAGS_APPEND="-parallel-jobs=$(nproc)"
  export ROCM_PATH=/opt/rocm
  # -fcf-protection is not supported by HIP, see
  # https://rocm.docs.amd.com/en/latest/reference/rocmcc/rocmcc.html#support-status-of-other-clang-options
  CXXFLAGS="${CXXFLAGS} -fcf-protection=none" \
  cmake \
    -Wno-dev \
    -B build \
    -S "$_dirname" \
    -DCMAKE_BUILD_TYPE=None \
    -DCMAKE_TOOLCHAIN_FILE="$srcdir/$_dirname/toolchain-linux.cmake" \
    -DCMAKE_INSTALL_PREFIX=/opt/rocm \
    -DROCM_PATH=/opt/rocm \
    -DSUPPORT_HIP=ON \
    -DSUPPORT_OMP=ON \
    -DSUPPORT_MPI=ON \
    -DBUILD_SHARED_LIBS=ON
  cmake --build build
}

package() {
  DESTDIR="$pkgdir" cmake --install build

  echo "/opt/rocm/$pkgname/lib" > "$pkgname.conf"
  install -Dm644 "$pkgname.conf" "$pkgdir/etc/ld.so.conf.d/$pkgname.conf"

  install -Dm644 "$_dirname/LICENSE.md" "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
